package org.adorsys.aderp.aderplogin.client;

import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;

public class ClientDetailsUserDetailsService implements UserDetailsService {
	private final ClientDetailsService clientDetailsService;

	public ClientDetailsUserDetailsService(ClientDetailsService clientDetailsService) {
		this.clientDetailsService = clientDetailsService;
	}

	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		ClientDetails clientDetails = clientDetailsService.loadClientByClientId(username);
		String clientSecret = clientDetails.getClientSecret() == null ? "" : clientDetails.getClientSecret();
		return new User(username, clientSecret, clientDetails.getAuthorities());
	}
}
